package com.example.htmlfx.component;

import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author gzkemays
 * @date 2021/8/5
 */
public class AlertComponent {
  /**
   * 异常错误的 area 类型提示
   *
   * @param e 异常
   * @param contentText 额外提示文本
   */
  public static void exceptionErrorArea(Exception e, String contentText) {
    Alert alert = new Alert(Alert.AlertType.ERROR);
    alert.setTitle("预料之外的错误");
    alert.setHeaderText("发生错误啦！/(ㄒoㄒ)/~~");
    alert.setContentText(contentText);
    // Create expandable Exception.
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    String exceptionText = sw.toString();
    Label label = new Label("Exception printStackTrace");
    TextArea textArea = new TextArea(exceptionText);
    textArea.setEditable(false);
    textArea.setWrapText(true);

    textArea.setMaxWidth(Double.MAX_VALUE);
    textArea.setMaxHeight(Double.MAX_VALUE);
    GridPane.setVgrow(textArea, Priority.ALWAYS);
    GridPane.setHgrow(textArea, Priority.ALWAYS);

    GridPane expContent = new GridPane();
    expContent.setMaxWidth(Double.MAX_VALUE);
    expContent.add(label, 0, 0);
    expContent.add(textArea, 0, 1);

    // Set expandable Exception into the dialog pane.
    alert.getDialogPane().setExpandableContent(expContent);
    alert.showAndWait();
  }

  /**
   * 正常、常见的错误提示
   *
   * @param headerText 自定义头部信息
   */
  public static void normalErrorTips(String headerText) {
    Alert alert = new Alert(Alert.AlertType.ERROR);
    alert.setTitle("错误提示");
    alert.setHeaderText(headerText);
    alert.setContentText(null);
    alert.showAndWait();
  }

  /**
   * 正常、常见的信息提示
   *
   * @param contentText 自定义文本信息
   */
  public static void normalInformationTips(String contentText) {
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setTitle("温馨提示");
    alert.setHeaderText(null);
    alert.setContentText(contentText);
    alert.showAndWait();
  }
}
